package sort;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/17 9:42
 * @Description
 */
public class Leet349 {
    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> res = new HashSet<>();
        List<Integer> collect = Arrays.stream(nums2).boxed().collect(Collectors.toList());
        for (int i : nums1) {
            if(collect.contains(i)){
                res.add(i);
            }
        }
        Integer[] objects = res.toArray(new Integer[0]);
        return Arrays.stream(objects).mapToInt(Integer::valueOf).toArray();
    }

    public static int[] intersection1(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int i:nums1){
            set1.add(i);
        }
        for(int i:nums2){
            if(set1.contains(i)){
                set2.add(i);
            }
        }
        int[] arr = new int[set2.size()];
        int j=0;
        for(int i:set2){
            arr[j++] = i;
        }
        return arr;
    }

    public static void main(String[] args) {
        int[] nums1 = {4,9,5};
        int[] nums2 = {9,4,9,8,4};
        for (int i : intersection1(nums1, nums2)) {
            System.out.println(i);
        }
    }
}
